﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Entity.Rules;
using NHibernate.Criterion;
using ZDSoft.QLMS.Entity;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity.CreateRule;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class StrategyExmaController : Controller
    {
        //
        // GET: /StrategyExma/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        public ActionResult Update(int id)
        {

            TestPaperRule tpr = new TestPaperRule().FindByID(id);
            TempData["Score"] = tpr.Grade;
            TempData["auditOpinion"] = tpr.AuditOpinion;
            TempData["Min"] = tpr.Min;
            TempData["ID"] = id;
            @TempData["Isactive"] = tpr.isPass;
            return View();
        }
        [HttpPost]
        public void Update(int ID, bool Ispass, string AuditOpinion, float score)
        {
            TestPaperRule tpr = new TestPaperRule().FindByID(ID);
            tpr.Grade = score;
            tpr.isPass = Ispass;
            tpr.AuditOpinion = AuditOpinion;
            tpr.Update();

        }

        public string GetCourseNameByTask(int id)
        {
            SetQuestionTask entity = new SetQuestionTask().FindByID(id);
            return entity.Course.Name;
        }
        public string GetPaged(int pageIndex, int pageSize, string strSearchProperty,
string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<TestPaperRule> tasks = new TestPaperRule().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
            string jsonStr = "[";

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (TestPaperRule entity in tasks)
            {
                jsonStr += "{\"ID\":" + pageNum + ",\"Course\":\"" + entity.Course.Name + "\",\"Grade\":\"" + entity.Grade + "\",\"isPass\":\"" + entity.isPass + "\"},";

            }
            jsonStr += "{\"ID\":" + pageNum + "}";
            jsonStr += "]";
            return jsonStr;
        }
        public JsonResult GetAllType()
        {
            IList<ZDSoft.QLMS.Entity.Type> types = new ZDSoft.QLMS.Entity.Type().FindAll();
            IList<ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode> tree = new List<ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode>();
            foreach (ZDSoft.QLMS.Entity.Type type in types)
            {
                ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode node = new ZTreeHelper<ZDSoft.QLMS.Entity.Type>.ZTreeNode();
                node.id = type.ID;
                node.tld = "type" + type.ID;
                node.icon = "../../Content/img/diy/1_close.png";
                node.name = type.Name;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllKnowledgePoint(int courseID)
        {
            Course course = new Course().FindByID(courseID);
            IList<KnowledgePoint> points = course.KnowledgePoints;
            IList<ZTreeHelper<KnowledgePoint>.ZTreeNode> tree = new List<ZTreeHelper<KnowledgePoint>.ZTreeNode>();
            foreach (KnowledgePoint point in points)
            {
                ZTreeHelper<KnowledgePoint>.ZTreeNode node = new ZTreeHelper<KnowledgePoint>.ZTreeNode();
                node.id = point.ID;
                node.tld = "know" + point.ID;
                node.icon = "../../Content/img/diy/1_close.png";
                node.name = point.Name;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public string GetStrategyedByID(int taskId)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("PublishMan", AppHelper.LoginedUser));
            queryConditions.Add(Expression.Eq("SetQuestionTask", new SetQuestionTask().FindByID(taskId)));
            TestPaperRule tpr = new TestPaperRule().Find(queryConditions)[0];
            string jsonStr = "[";
            foreach (TypeRule tr in tpr.TypeRules)
            {
                jsonStr += "{\"weight\":\"" + tr.Weight + "\",\"id\":\"" + tr.ID + "\",\"tld\":\"tepy" + tr.Type.ID + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + tr.Type.Name + "\"";
                if (tr.KnowledgePointRules != null && tr.KnowledgePointRules.Count != 0)
                {
                    jsonStr += ",\"children\":[";
                    foreach (KnowledgePointRule klp in tr.KnowledgePointRules)
                    {
                        jsonStr += "{\"weight\":\"" + klp.Weight + "\",\"id\":\"" + klp.ID + "\",\"tld\":\"know" + klp.KnowledgePoint.ID + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + klp.KnowledgePoint.Name + "\"";
                        if (klp.Details != null && klp.Details.Count != 0)
                        {

                            jsonStr += ",\"children\":[";
                            foreach (Detail detail in klp.Details)
                            {
                                jsonStr += "{\"num\":\"" + detail.Num + "\",\"weight\":\"" + detail.Weight + "\",\"id\":\"" + detail.ID + "\",\"tld\":\"diff" + (int)detail.DefficultDegree + "\",\"icon\":\"../../Content/img/diy/1_close.png\",\"name\":\"" + detail.DefficultDegree + "\"},";
                            }
                            jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
                            jsonStr += "]";
                        }
                        jsonStr += "},";
                    }
                    jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
                    jsonStr += "]";
                }
                jsonStr += "},";
            }
            jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
            jsonStr += "]";
            return jsonStr;
        }
    }
}
